package h0827;

import java.util.Arrays;
import java.util.HashSet;

public class LongestContinuousSequence2 {
    public static void main(String[] args) {
        int[] nums = {100,4,200,1,3,2};
        int a = getRes(nums);
        System.out.println(a);
    }
    private static int getRes(int[] nums) {
       //首先要去重
        //其次判断每个元素是否是开头。具体做法是判断x-1是否在集合内
        //然后根据每个开头更新最大长度
        HashSet<Integer> set=new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }
        int maxLen=0;
        for (int i = 0; i < nums.length; i++) {
            int a = nums[i] - 1;

            if (!set.contains(a)){
                int tempLen=0;
                int m=nums[i];
                while (set.contains(m)){
                    tempLen++;
                    m++;
                }
                maxLen=Math.max(maxLen,tempLen);
            }else {
                continue;
            }
            //不是开头则直接跳过
        }
        return maxLen;
    }
}
